//
//  YFRankApi.m
//  CoollangTennisBall
//
//  Created by Coollang on 2018/1/13.
//  Copyright © 2018年 Coollang-YF. All rights reserved.
//

#import "YFRankApi.h"
#import "RankingModel.h"
#import "MyRankInfoModel.h"

@implementation YFRankApi

/**
 * 获取排行榜列表
 *
 * type: 2为分数排名,1为次数排名,0为速度(0暂时不用了)
 * timePeriod: 0为月排名,1为累计排名,2为周排名
 */
+ (instancetype)requestRankinglistWithRankType:(NSString *)type timePeriod:(NSString *)timePeriod {
    NSDictionary *param = @{@"Type": type,
                            @"Class": timePeriod};
    YFRankApi *getRanklistApi = [YFRankApi requestApiWithType:kRankNewGetListAPi andParam:param];
    return getRanklistApi;
}

// 排行榜列表
+ (instancetype)requestRankinglistWithRankType:(NSString *)type timePeriod:(NSString *)timePeriod completed:(void(^)(BOOL success, NSArray <RankingModel *> *rankItems,MyRankInfoModel *myRankInfoItem))completeBlock {
    NSDictionary *param = @{@"Type": type,
                            @"Class": timePeriod};
    YFRankApi *getRanklistApi = [YFRankApi requestApiWithType:kRankNewGetListAPi andParam:param];
    
    [[YFNetworkingService sharedInstance] sendRequest:getRanklistApi callback:^(BOOL success, id result, NSError *error) {
        if (success) {
            NSDictionary *dic  = [[result responseInfo] objectForKey:@"MyRankInfo"];
            MyRankInfoModel *myRankInfoItem = [MyRankInfoModel myRankinfoModelWithDict:dic];
            NSMutableArray *mutaArray = @[].mutableCopy;
            NSArray *array = [[result responseInfo] objectForKey:@"RankList"];
            for (NSDictionary *dic in array) {
                RankingModel *model = [RankingModel rankIngModelWithDict:dic];
                [mutaArray addObject:model];
            }
            if (completeBlock) {
                completeBlock(YES,mutaArray.copy,myRankInfoItem);
            }
        }else {
            if (completeBlock) {
                completeBlock(NO,nil,nil);
            }
        }
    }];
    
    return getRanklistApi;
}

- (NSArray<RankingModel *> *)rankItems {
    if (self.apiType == kRankNewGetListAPi && _rankItems == nil) {
        NSMutableArray *mutaArray = @[].mutableCopy;
        NSArray *array = [self.responseInfo objectForKey:@"RankList"];
        for (NSDictionary *dic in array) {
            RankingModel *model = [RankingModel rankIngModelWithDict:dic];
            [mutaArray addObject:model];
        }
        _rankItems = mutaArray.copy;
    }
    return _rankItems;
}
- (MyRankInfoModel *)myRankInfoItem {
    if (self.apiType == kRankNewGetListAPi && _myRankInfoItem == nil){
        NSDictionary *dic  = [self.responseInfo objectForKey:@"MyRankInfo"];
        _myRankInfoItem = [MyRankInfoModel myRankinfoModelWithDict:dic];
    }
    return _myRankInfoItem;
}

+ (instancetype)requestOtherRankingType:(RankOtherRequestType)type andUserId:(NSString  *)userId {
    RequestApiType apiType = kRankNewAddLikeAPi;
    switch (type) {
        case kRankAddlike:
            apiType = kRankNewAddLikeAPi;
            break;
        case kRankDellike:
            apiType = kRankNewCancelLikeAPi;
            break;
        case kRankGetUserinfo:
            apiType = kRankNewGetUserRankInfoAPi;
            break;
        default:
            break;
    }
    return [YFRankApi requestApiWithType:apiType andParam:@{@"userID": userId}];
}





@end
